package com.atguigu.jxc.service.impl;/*
 *@author 尹凯瑞
 *@date 2020/12/2 19:51
 */

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private LogService logService;

    //分页查询供应商信息
    @Override
    public Map<String, Object> listSupplier(Integer page, Integer rows, String supplierName) {
        //创建一个集合保存信息
        Map<String,Object> map = new HashMap<>();
        //当前页
        page  = page == 0 ? 1 : page;
        //每页从第几条数据开始
        int offSet = (page - 1) * rows;
        //查询所有的供应商
        List<Supplier> suppliersList =  supplierDao.getSupplierList(offSet, rows, supplierName);
        //将信息保存到map
        map.put("rows",suppliersList);
        map.put("total",supplierDao.getSupplierCount(supplierName));
        //日志
        logService.save(new Log(Log.SELECT_ACTION, "分页查询供应商信息"));
        //返回
        return map;
    }
    //添加或保存供应商
    @Override
    public ServiceVO saveOrUpdate(Supplier supplier) {


        if(supplier.getSupplierId() != null){
            supplierDao.updateSupplier(supplier);
        }else{
            supplierDao.saveSupplier(supplier);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
    //删除供应商信息，支持批量删除
    @Override
    public ServiceVO deleteSupplier(String ids) {
        String[] split = ids.split(",");//转化为数组
        supplierDao.deleteSupplier(split);//
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
